package com.wgchao.amc.controller.base;

import com.wgchao.amc.autho.AuthoInfoCenter;
import com.wgchao.amc.autho.UserInfo;
import com.wgchao.amc.common.MenuAutho;
import com.wgchao.amc.common.PageData;
import com.wgchao.amc.common.ResultCommon;
import com.wgchao.amc.common.validate.Save;
import com.wgchao.amc.common.validate.Update;
import com.wgchao.amc.mapstruct.dto.SysBaseAgencyDTO;
import com.wgchao.amc.service.AgencyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: AgencyController
 * @author: wgchao
 * @createTime: 2019/10/14 8:57 AM
 * @Description: 机构信息管理
 */
@RestController
@RequestMapping("/amc/sys/base/agency")
@Api(tags = "机构信息管理")
public class AgencyController {
    @Autowired
    private AgencyService agencyService;

    @PostMapping("/save")
    @ApiOperation(value = "新增机构")
    @MenuAutho(desc = "新增机构")
    @ApiParam(name = "agencyDTO", value = "机构信息", required = true)
    public ResultCommon<SysBaseAgencyDTO> save(@RequestBody @Validated(value = {Save.class}) SysBaseAgencyDTO agencyDTO,
                                               BindingResult bindingResult,
                                               HttpSession session) throws Exception{
        if(bindingResult.hasErrors()){
            String error = "";
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                error += (fieldError.getDefaultMessage()+",");
            }
            return ResultCommon.fail(error);
        }
        AuthoInfoCenter authoInfoCenter = AuthoInfoCenter.getInstance(session);
        return agencyService.save(agencyDTO, authoInfoCenter);
    }

    @GetMapping("/search/{agencyCode}")
    @ApiOperation(value = "根据机构编号查询机构详情")
    @MenuAutho(desc = "根据机构编号查询机构详情")
    @ApiParam(name = "agencyCode", value = "机构编号", required = true)
    public ResultCommon<SysBaseAgencyDTO> search(@PathVariable String agencyCode) throws Exception{
        return agencyService.search(agencyCode);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询机构信息")
    @MenuAutho(desc = "分页查询机构信息")
    @ApiParam(name = "agencyCode", value = "机构编号", required = true)
    public ResultCommon<PageData<SysBaseAgencyDTO>> page(int page, int limit,
                                                         @RequestParam(required = false) String agencyName,
                                                         HttpSession session) throws Exception{
        AuthoInfoCenter authoInfoCenter = AuthoInfoCenter.getInstance(session);
        return agencyService.page(page, limit, agencyName, authoInfoCenter);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新机构信息", notes = "可更新机构名称、地址、备注")
    @MenuAutho(desc = "更新机构信息")
    @ApiParam(name = "agencyDTO", value = "机构信息", required = true)
    public ResultCommon<SysBaseAgencyDTO> update(@RequestBody @Validated(value = {Update.class}) SysBaseAgencyDTO agencyDTO,
                                               BindingResult bindingResult,
                                                 HttpSession session) throws Exception{
        if(bindingResult.hasErrors()){
            String error = "";
            for (FieldError fieldError : bindingResult.getFieldErrors()) {
                error += (fieldError.getDefaultMessage()+",");
            }
            return ResultCommon.fail(error);
        }
        AuthoInfoCenter authoInfoCenter = AuthoInfoCenter.getInstance(session);
        return agencyService.update(agencyDTO, authoInfoCenter);
    }

    @DeleteMapping("/deletes")
    @ApiOperation(value = "删除机构信息", notes = "同时删除子机构")
    @MenuAutho(desc = "删除机构信息")
    @ApiParam(name = "agencyCodes", value = "机构编号列表", required = true)
    public ResultCommon deletes(@RequestBody List<String> agencyCodes, HttpSession session) throws Exception{
        if(agencyCodes==null || agencyCodes.isEmpty()){
            return ResultCommon.fail("待删除机构编号缺失");
        }
        AuthoInfoCenter authoInfoCenter = AuthoInfoCenter.getInstance(session);
        return agencyService.deletes(agencyCodes, authoInfoCenter);
    }

    @PutMapping("/cancels")
    @ApiOperation(value = "注销机构信息", notes = "同时注销子机构")
    @MenuAutho(desc = "注销机构信息")
    @ApiParam(name = "agencyCodes", value = "机构编号列表", required = true)
    public ResultCommon cancels(@RequestBody List<String> agencyCodes, HttpSession session) throws Exception{
        if(agencyCodes==null || agencyCodes.isEmpty()){
            return ResultCommon.fail("待注销机构编号缺失");
        }
        AuthoInfoCenter authoInfoCenter = AuthoInfoCenter.getInstance(session);
        return agencyService.cancels(agencyCodes, authoInfoCenter);
    }

    @GetMapping("/search/child/list/{agencyCode}")
    @ApiOperation(value = "根据机构编号查询机构以及子机构列表")
    @MenuAutho(desc = "根据机构编号查询机构以及子机构列表")
    @ApiParam(name = "agencyCode", value = "机构编号", required = true)
    public ResultCommon<List<SysBaseAgencyDTO>> searchChilds(@PathVariable String agencyCode) throws Exception{
        return agencyService.searchChilds(agencyCode);
    }

    @GetMapping("/search/child/tree/{agencyCode}")
    @ApiOperation(value = "根据机构编号查询机构以及子机构列表", notes = "树状结构")
    @MenuAutho(desc = "根据机构编号查询机构以及子机构列表(树状结构)")
    @ApiParam(name = "agencyCode", value = "机构编号", required = true)
    public ResultCommon<SysBaseAgencyDTO> searchChildsTree(@PathVariable String agencyCode) throws Exception{
        return agencyService.searchChildsTree(agencyCode);
    }

}
